package com.inspectortime.webapp.inspector.reports;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.mkode.util.java.lang.ClassUtils;

public class ReportDateValidator  implements Validator {
	
	public boolean supports(Class clazz) {
		return ClassUtils.isInstanceOf(clazz, ReportDateForm.class);
	}
	
	public void validate(Object obj, Errors errors) {
		ReportDateForm form = (ReportDateForm) obj;
		
		ValidationUtils.rejectIfEmpty(errors, "startDate","required");
		ValidationUtils.rejectIfEmpty(errors, "endDate","required");
		
		if (form.getStartDate() != null && form.getEndDate() != null) {
			if (form.getStartDate().after(form.getEndDate())) {
				errors.rejectValue("endDate", "startEndDate.invalid");
			}
		}
	}
	
	
}
